﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using fuzhu;
using Dapper;
using LKU8.shoukuan.Model;

namespace LKU8.shoukuan
{
    public class FaHuoService
    {
        
        /// <summary>
        /// 判断计划是否存在
        /// </summary>
        /// <param name="cInvCode">存货编码</param>
        /// <param name="constr">数据库连接字符串</param>
        /// <returns>存货编码是否存在</returns>
        public static bool IsPlanExists(string cSoCode, string constr)
        {
            using (SqlConnection conn = new SqlConnection(constr))
            {
                string sql = @"SELECT COUNT(1) FROM mps_netdemand WHERE SoCode = @cSoCode";
                int count = conn.Query<int>(sql, new { cSoCode = cSoCode }).FirstOrDefault(); ; 
                return count > 0 ? true : false; 
            }
        }


        /// <summary>
        /// 判断销售订单是否存在
        /// </summary>
        /// <param name="cInvCode">存货编码</param>
        /// <param name="constr">数据库连接字符串</param>
        /// <returns>存货编码是否存在</returns>
        public static bool IsSoCodeExists(string cSoCode, string constr)
        {
            using (SqlConnection conn = new SqlConnection(constr))
            {
                string sql = @"SELECT COUNT(1) FROM so_somain WHERE csocode = @cSoCode";
                int count = conn.Query<int>(sql, new { cSoCode = cSoCode }).FirstOrDefault(); ;
                return count > 0 ? true : false;
            }
        }

        /// <summary>
        /// 判断发货单是否存在
        /// </summary>
        /// <param name="cInvCode">存货编码</param>
        /// <param name="constr">数据库连接字符串</param>
        /// <returns>存货编码是否存在</returns>
        public static bool IsDisPatchExists(WanJi_FaHuo_View entity, string constr)
        {
            using (SqlConnection conn = new SqlConnection(constr))
            {
                string sql = @"SELECT COUNT(1) FROM dispatchlist WHERE dDate = @dDate and cSOCode = @cSoCode ";
                int count = conn.Query<int>(sql,  entity).FirstOrDefault(); ;
                return count > 0 ? true : false;
            }
        }

        /// <summary>
        /// 读取销售订单行id
        /// </summary>
        /// <param name="cInvCode">存货编码</param>
        /// <param name="constr">数据库连接字符串</param>
        /// <returns>存货编码是否存在</returns>
        public static int GetISosid(WanJi_FaHuo_View entity, string constr)
        {
            using (SqlConnection conn = new SqlConnection(constr))
            {
                string sql = @"SELECT b.isosid FROM so_somain a inner join so_sodetails b on a.id= b.id
WHERE  a.cSOCode = @cSoCode and b.cinvcode=@cInvCode";
                return  conn.Query<int>(sql, entity).FirstOrDefault(); ;
             
            }
        }

        /// <summary>
        /// 读取销售订单的表体
        /// </summary>
        /// <param name="cInvCode">销售订单号</param>
        /// <param name="constr">数据库连接字符串</param>
        /// <returns>销售存货档案和isosid</returns>
        public static List<WanJi_FaHuo_View> GetSodetaillst(string cSoCode, string constr)
        {
            using (SqlConnection conn = new SqlConnection(constr))
            {
                string sql = @"SELECT b.cInvCode,b.iSosid from  so_somain a inner join so_sodetails b on a.id= b.id
WHERE  a.cSOCode = @cSoCode ";
               return  conn.Query<WanJi_FaHuo_View>(sql, new { cSoCode = cSoCode }).ToList() ;
                
            }
        }

       }


         

       
 
}
